package com.jajahome.pop;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.jajahome.R;
import com.jajahome.constant.Constant;

public class PopVIPFilter {
    private Context context;
    private View view;
    private PopupWindow mPopupWindow;
    private ImageView iv_close;
    private WebView webView;
    private WebSettings webSettings;

    public PopVIPFilter(Context context, View view) {
        this.context = context;
        this.view = view;
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(context).inflate(R.layout.pop_vip,null);
        iv_close = (ImageView) view.findViewById(R.id.iv_close);
        webView = (WebView)view.findViewById(R.id.pop_vip);
        webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl(Constant.VIP_RULE);
        mPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    /**
     * 显示popupwindow
     */
    public void show() {
        if (mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        } else {
            mPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        }
    }


    /**
     * 关闭popupwindow
     */
    public void dismiss() {
        if (mPopupWindow.isShowing()) mPopupWindow.dismiss();
    }

    protected void showToast(String str) {
        //去换行
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}
